package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventYzwfxy;
import com.ruoyi.event.domain.bo.EventYzwfxyBo;
import com.ruoyi.event.domain.vo.EventYzwfxyVo;
import com.ruoyi.event.mapper.EventYzwfxyMapper;
import com.ruoyi.event.service.IEventYzwfxyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 严重违反协议Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventYzwfxyServiceImpl implements IEventYzwfxyService {

    private final EventYzwfxyMapper baseMapper;

    /**
     * 查询严重违反协议
     */
    @Override
    public EventYzwfxyVo queryById(Long yzwfxyId) {
        return baseMapper.selectVoById(yzwfxyId);
    }

    /**
     * 查询严重违反协议列表
     */
    @Override
    public TableDataInfo<EventYzwfxyVo> queryPageList(EventYzwfxyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventYzwfxy> lqw = buildQueryWrapper(bo);
        Page<EventYzwfxyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询严重违反协议列表
     */
    @Override
    public List<EventYzwfxyVo> queryList(EventYzwfxyBo bo) {
        LambdaQueryWrapper<EventYzwfxy> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventYzwfxy> buildQueryWrapper(EventYzwfxyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventYzwfxy> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventYzwfxy::getUserId, bo.getUserId());
        lqw.eq(bo.getWfxysj() != null, EventYzwfxy::getWfxysj, bo.getWfxysj());
        lqw.eq(StringUtils.isNotBlank(bo.getWfxylx()), EventYzwfxy::getWfxylx, bo.getWfxylx());
        lqw.eq(StringUtils.isNotBlank(bo.getWfxyss()), EventYzwfxy::getWfxyss, bo.getWfxyss());
        lqw.eq(StringUtils.isNotBlank(bo.getClqk()), EventYzwfxy::getClqk, bo.getClqk());
        lqw.eq(StringUtils.isNotBlank(bo.getQjs()), EventYzwfxy::getQjs, bo.getQjs());
        return lqw;
    }

    /**
     * 新增严重违反协议
     */
    @Override
    public Boolean insertByBo(EventYzwfxyBo bo) {
        EventYzwfxy add = BeanUtil.toBean(bo, EventYzwfxy.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setYzwfxyId(add.getYzwfxyId());
        }
        return flag;
    }

    /**
     * 修改严重违反协议
     */
    @Override
    public Boolean updateByBo(EventYzwfxyBo bo) {
        EventYzwfxy update = BeanUtil.toBean(bo, EventYzwfxy.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventYzwfxy entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除严重违反协议
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
